package com.xpsoft.oa.workflow.event;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.admin.ContractBorrow;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.admin.ContractBorrowService;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ContractService;
import com.xpsoft.oa.service.project.ProjectService;
import com.xpsoft.oa.service.system.AppUserService;
import com.xpsoft.oa.service.system.DepartmentService;

public class ContractBorrowFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(ContractBorrowFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String userId = (String)pi.getVariable("contractBorrow.applyUser.userId");
		String depId = (String)pi.getVariable("contractBorrow.dept.depId");
		String proNeed = (String)pi.getVariable("contractBorrow.proNeed");
		String proId = (String)pi.getVariable("contractBorrow.project.id");
		String borrowLimit = (String)pi.getVariable("contractBorrow.borrowLimit");
		String contractId = (String)pi.getVariable("contractBorrow.contract.id");
		String borrowReason = (String)pi.getVariable("contractBorrow.borrowReason");
		String remark = (String)pi.getVariable("contractBorrow.remark");
		Date preReturnDate = (Date)pi.getVariable("contractBorrow.preReturnDate");
		
		ContractBorrow contractBorrow = new ContractBorrow();
		contractBorrow.setProNeed(proNeed);
		contractBorrow.setBorrowLimit(borrowLimit);
		contractBorrow.setBorrowReason(borrowReason);
		contractBorrow.setRemark(remark);
		contractBorrow.setPreReturnDate(preReturnDate);
		if(StringUtils.isNotBlank(userId)){
			AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
			contractBorrow.setApplyUser(appUserService.get(Long.valueOf(userId)));
		}
		if(StringUtils.isNotBlank(depId)){
			DepartmentService departmentService = (DepartmentService)AppUtil.getBean("departmentService");
			contractBorrow.setDept(departmentService.get(Long.valueOf(depId)));
		}
		if(StringUtils.isNotBlank(proId)){
			ProjectService projectService = (ProjectService)AppUtil.getBean("projectService");
			contractBorrow.setProject(projectService.get(Long.valueOf(proId)));
		}
		if(StringUtils.isNotBlank(contractId)){
			ContractService contractService = (ContractService)AppUtil.getBean("contractService");
			contractBorrow.setContract(contractService.get(Long.valueOf(contractId)));
		}

		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		contractBorrow.setProcessRunId(processRun.getRunId());
		
		ContractBorrowService contractBorrowService = (ContractBorrowService)AppUtil.getBean("contractBorrowService");
		contractBorrowService.save(contractBorrow);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
